function results = multiuser_scheduling_queuing(varargin)
% 多用户调度中的排队分析函数
% 功能：分析轮询调度和机会调度的排队性能
%
% 输入参数：
%   'arrival_rates' - 到达率向量 (默认: 0.1:0.1:0.9)
%   'num_users' - 用户数量 (默认: 4)
%   'num_simulations' - 仿真次数 (默认: 1000)
%   'plot_results' - 是否绘制结果 (默认: true)
%
% 输出：
%   results - 包含分析结果的结构体
%
% 示例：
%   results = multiuser_scheduling_queuing('arrival_rates', 0.1:0.1:0.9, 'num_users', 4);

% 参数解析
p = inputParser;
addParameter(p, 'arrival_rates', 0.1:0.1:0.9);
addParameter(p, 'num_users', 4);
addParameter(p, 'num_simulations', 1000);
addParameter(p, 'plot_results', true);
parse(p, varargin{:});

arrival_rates = p.Results.arrival_rates;
num_users = p.Results.num_users;
num_simulations = p.Results.num_simulations;
plot_results = p.Results.plot_results;

% 添加路径
addpath('../Common');

% 获取颜色定义
colors = color_definitions();

fprintf('=== 多用户调度中的排队分析 ===\n');

% 多用户轮询调度
user_arrival_rates = [0.2, 0.3, 0.15, 0.25]; % 各用户到达率
user_service_rates = ones(1, num_users); % 各用户服务率

% 轮询调度仿真
rr_delays = zeros(num_users, length(arrival_rates));
rr_throughputs = zeros(num_users, length(arrival_rates));

for user = 1:num_users
    for lambda_idx = 1:length(arrival_rates)
        % 调整用户到达率
        adjusted_lambda = user_arrival_rates(user) * arrival_rates(lambda_idx) / mean(user_arrival_rates);
        mu = user_service_rates(user);
        
        if adjusted_lambda < mu
            % M/M/1理论值
            avg_delay = 1 / (mu - adjusted_lambda);
            throughput = adjusted_lambda;
        else
            avg_delay = inf;
            throughput = 0;
        end
        
        rr_delays(user, lambda_idx) = avg_delay;
        rr_throughputs(user, lambda_idx) = throughput;
    end
end

% 最大载干比调度 (机会调度)
max_cir_delays = zeros(1, length(arrival_rates));
max_cir_throughputs = zeros(1, length(arrival_rates));

for lambda_idx = 1:length(arrival_rates)
    lambda = arrival_rates(lambda_idx);
    
    % 机会调度增益
    num_samples = num_simulations;
    channel_gains = zeros(num_samples, num_users);
    
    for i = 1:num_samples
        channel_gains(i, :) = abs(sqrt(0.5) * (randn(1, num_users) + 1i * randn(1, num_users))).^2;
    end
    
    % 选择最佳用户
    [max_gains, ~] = max(channel_gains, [], 2);
    avg_max_gain = mean(max_gains);
    
    % 等效服务率 (考虑多用户分集)
    effective_mu = mean(user_service_rates) * avg_max_gain;
    
    if lambda < effective_mu
        avg_delay = 1 / (effective_mu - lambda);
        throughput = lambda;
    else
        avg_delay = inf;
        throughput = 0;
    end
    
    max_cir_delays(lambda_idx) = avg_delay;
    max_cir_throughputs(lambda_idx) = throughput;
end

% 绘制结果
if plot_results
    figure('Name', '多用户调度排队分析', 'Position', [200, 200, 1200, 800]);
    
    subplot(2,2,1);
    for user = 1:num_users
        plot(arrival_rates, rr_delays(user, :), ['-', colors(user)], 'LineWidth', 2);
        hold on;
    end
    grid on;
    xlabel('系统到达率');
    ylabel('平均时延');
    title('轮询调度: 各用户时延');
    legend(arrayfun(@(x) sprintf('用户%d', x), 1:num_users, 'UniformOutput', false));
    
    subplot(2,2,2);
    plot(arrival_rates, mean(rr_delays, 1), 'b-o', 'LineWidth', 2);
    hold on;
    plot(arrival_rates, max_cir_delays, 'r-s', 'LineWidth', 2);
    grid on;
    xlabel('系统到达率');
    ylabel('平均时延');
    title('调度算法比较');
    legend('轮询调度', '最大载干比调度');
    
    subplot(2,2,3);
    for user = 1:num_users
        plot(arrival_rates, rr_throughputs(user, :), ['-', colors(user)], 'LineWidth', 2);
        hold on;
    end
    grid on;
    xlabel('系统到达率');
    ylabel('吞吐量');
    title('轮询调度: 各用户吞吐量');
    
    subplot(2,2,4);
    plot(arrival_rates, mean(rr_throughputs, 1), 'b-o', 'LineWidth', 2);
    hold on;
    plot(arrival_rates, max_cir_throughputs, 'r-s', 'LineWidth', 2);
    grid on;
    xlabel('系统到达率');
    ylabel('系统吞吐量');
    title('调度算法吞吐量比较');
    legend('轮询调度', '最大载干比调度');
end

% 组织结果
results = struct();
results.arrival_rates = arrival_rates;
results.num_users = num_users;
results.rr_delays = rr_delays;
results.rr_throughputs = rr_throughputs;
results.max_cir_delays = max_cir_delays;
results.max_cir_throughputs = max_cir_throughputs;
results.user_arrival_rates = user_arrival_rates;
results.colors = colors;

fprintf('多用户调度中的排队分析完成\n');
end